package com.herenit.beijing.dh2011.business.base;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class BaseManager<T> implements IBaseManager<T>{
	@PersistenceContext(unitName = "haha")
    public EntityManager em;

	@Override
	public void save(T t) {
		em.persist(t);
		
	}

	@Override
	public void delete(T t) {
		em.remove(t);
	}

	@Override
	public void update(T t) {
		em.merge(t);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> findAll(Class<T> clazz) {
		
		StringBuilder jpql=new StringBuilder();
		jpql.append("select t from ");
		jpql.append(clazz.getName());
		
		
		
		return em.createQuery(jpql.toString()).getResultList();
	}

	@Override
	public T findById(Class<T> clazz,Object id) {
		
		return em.find(clazz, id);
	}
	

}
